package com.jack.lib.ui.widget

import android.content.Context
import android.widget.FrameLayout
import kotlin.jvm.JvmOverloads
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import com.jack.lib.ui.R
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:26
 */
open class JFrameLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var mJRadio = false
    private var mJAutoPress = false
    private var mClickGap = 0
    private var mOldClickTime: Long = 0
    private var mOnItemSelectedListener: OnItemSelectedListener? = null


    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JFrameLayout)
        parseAttr(typedArray)
        typedArray.recycle()
    }

    private fun parseAttr(typedArray: TypedArray) {
        mJRadio = typedArray.getBoolean(R.styleable.JFrameLayout_JRadio, false)
        mJAutoPress = typedArray.getBoolean(R.styleable.JFrameLayout_JAutoPress, false)
        mClickGap = typedArray.getInteger(R.styleable.JFrameLayout_JClickGap, 0)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        if (mJRadio) {
            radio()
        }
    }

    override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams) {
        super.addView(child, index, params)
        if (mJRadio) {
            radio()
        }
    }

    override fun removeView(view: View) {
        super.removeView(view)
        if (mJRadio) {
            radio()
        }
    }

    override fun removeViewAt(index: Int) {
        super.removeViewAt(index)
        if (mJRadio) {
            radio()
        }
    }

    private fun radio() {
        for (i in 0 until childCount) {
            val view = getChildAt(i)
            view.setOnClickListener { v: View? ->
                for (j in 0 until childCount) {
                    if (i == j && !getChildAt(j).isSelected && mOnItemSelectedListener != null) {
                        mOnItemSelectedListener!!.OnItemSelected(j)
                    }
                    getChildAt(j).isSelected = i == j
                }
            }
        }
    }

    interface OnItemSelectedListener {
        fun OnItemSelected(position: Int)
    }

    fun setOnItemSelectedListener(onItemSelectedListener: OnItemSelectedListener?) {
        mOnItemSelectedListener = onItemSelectedListener
    }

    override fun performClick(): Boolean {
        val millis = System.currentTimeMillis()
        if (millis - mOldClickTime < mClickGap) {
            return false
        }
        mOldClickTime = millis
        return super.performClick()
    }

    override fun setPressed(pressed: Boolean) {
        super.setPressed(pressed)
        val alpha = if (pressed && isClickable) 0.8f else 1f
        if (mJAutoPress && alpha != getAlpha()) {
            setAlpha(alpha)
        }
    }
}